package com.sgb.zhaocai.view.demand.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.alimaplibrary.SelectAddressActivity;
import com.sgb.link.router.PathLogin;
import com.sgb.link.utils.JiCaiEditInputFilter;
import com.sgb.link.utils.NumericalTransformationTool;
import com.sgb.link.view.widget.ExpandedTextView;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.databinding.ActivityDemandOfferBinding;
import com.sgb.zhaocai.entity.CustomBaseBaen;
import com.sgb.zhaocai.entity.DemandOfferBean;
import com.sgb.zhaocai.entity.MapLongAndLatEntity;
import com.sgb.zhaocai.entity.NoticeOfferBean;
import com.sgb.zhaocai.entity.OfferApplyListBean;
import com.sgb.zhaocai.entity.OfferDetailsEntitly;
import com.sgb.zhaocai.view.demand.adapter.DemandImgAdapter;
import com.sgb.zhaocai.viewmodel.DemandOfferModel;
import com.swgk.core.base.AppActivity;
import com.swgk.core.common.utils.ActivityJump;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.customview.PhotoViewActivity;
import com.swgk.core.dialog.CommonDialog;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.GlideEngine;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.CityPickBuilder;
import com.swgk.core.view.CityPickView;
import com.swgk.core.view.areapopup.AddressEntity;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class DemandOfferActivity extends AppActivity implements ItemInputTextView.OnWholeItemClickListener {

    private ActivityDemandOfferBinding mBinding;
    private DemandOfferModel mModel;
    private String acttype = "";
    private String id = "";
    private String type = "";
    private DemandImgAdapter mPicAdapter;
    private String time = "";
    private int com_type;
    private String name = "";
    private List<String> files;
    DemandOfferBean bean = new DemandOfferBean();
    // 地址一级联动数据
    private List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> addressEntityList;
    // 地址二级联动数据
    private final List<List<AddressEntity<List<AddressEntity>>>> opt2 = new ArrayList<>();
    // 地址三级联动数据
    private final List<List<List<AddressEntity>>> opt3 = new ArrayList<>();
    private String lat = "";
    private String lng = "";
    private int cityPosition1;
    private int cityPosition2;
    private int cityPosition3;
    private List<String> addresscode = new ArrayList<>();
    private String applyCompany = "";
    private double addrDetailLat;
    private double addrDetailLng;

    public static void start(Context context, String id, String type, String acttype) {
        Intent intent = new Intent();
        intent.putExtra("id", id);
        intent.putExtra("type", type);
        intent.putExtra("acttype", acttype);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.setClass(context, DemandOfferActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_demand_offer);
        mModel = new ViewModelProvider(this).get(DemandOfferModel.class);
        //类型 0个人 1企业 2团队
        com_type = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
        if (com_type == 1) {
            name = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NAME, "");
            bean.setApplyCompany(SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO, "") + "");
            bean.setApplyCompanyName(name);
        } else {
            name = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.REAL_NAME, "");
        }
        mBinding.includeTitle.ivBack.setOnClickListener(v -> onBackPressed());
        mBinding.includeTitle.titleView.setText("需求报价");
        //如果存在url表示从详情界面跳转过来，否就是从原生界面跳转
        acttype = getIntent().getStringExtra("acttype");
        id = getIntent().getStringExtra("id");
        type = getIntent().getStringExtra("type");
        Log.e("tag", "id: " + id + "type: " + type + "acttype: " + acttype);
        ViewOnClick();//初始化点击事件
        imtvView();//图文描述模块view初始化
    }

    @Override
    protected void init() {
        super.init();
        //获取订单详情方法
        mModel.getOfferDetails(this, id, type);
    }

    private void ViewOnClick() {
        //价格
        mBinding.price.setFilters(new JiCaiEditInputFilter[]{new JiCaiEditInputFilter(9999999999.99, 0.01,2)});
        //联系电话
        NumericalTransformationTool.OnItemInputTextViewChange(mBinding.phone);
        mBinding.phone.setInfilterType(ItemInputTextView.TYPE_PHONE);//纯数字
        mBinding.address.setOnWholeItemClickListener(this);//地址选择
        mBinding.detailedAddress.setOnWholeItemClickListener(this);//详细地址
        //提交数据
        mBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    showDialog3();
                    return;
                }else {
                    //报价时间小于当天的最大时间才可以报价
                    if (getDataTime()) {
                        subData();
                    }
                }
            }
        });
        //跳转到企业网站
        mBinding.demandCom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseApi.WebSiteInfo(applyCompany, 1);
            }
        });
        //跳转到地图界面
        mBinding.llMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Intent intent = new Intent(getActivity(), SelectAddressActivity.class);
            intent.putExtra(SelectAddressActivity.LONGTITUDE,addrDetailLng);
            intent.putExtra(SelectAddressActivity.LATITUDE,addrDetailLat);
            intent.putExtra(SelectAddressActivity.SHOW_VIEW,"2");
            intent.putExtra(SelectAddressActivity.TITLE,"项目地址");
            startActivity(intent);
            }
        });
    }

    @Override
    public void OnWholeItemClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.address) {//地址
            if (addressEntityList == null) {
                mModel.getAddressModel(this);
            } else {
                showCityPop();
            }
        } else if (viewId == R.id.detailed_address) {//详细地址
            if (!TextUtils.isEmpty(mBinding.address.getContent().toString())) {
                Intent intent = new Intent();
                intent.setClass(this, ZhaoCaiMapAddressActivity.class);
                intent.putExtra(ZhaoCaiMapAddressActivity.TITLE, "详细地址");
                intent.putExtra(ZhaoCaiMapAddressActivity.HINT, "请输入地址");
                intent.putExtra(ZhaoCaiMapAddressActivity.SAVE_TEXT, "确定");
                intent.putExtra(ZhaoCaiMapAddressActivity.LATITUDE, Double.parseDouble(lat));
                intent.putExtra(ZhaoCaiMapAddressActivity.LONGTITUDE, Double.parseDouble(lng));
                startActivityForResult(intent, 255);
            } else {
                MToast.showToast(this, "请先选择地址");
            }
        }
    }

    //提交数据信息
    private void subData() {
        if (TextUtils.isEmpty(mBinding.price.getText().toString())) {
            MToast.showToast(this, "请输入" + mBinding.offerPriceName.getText().toString());
            return;
        } else {
            bean.setPrice(mBinding.price.getText().toString());
        }
        if (TextUtils.isEmpty(mBinding.userName.getContent())) {
            MToast.showToast(this, "请输入联系人");
            return;
        } else {
            bean.setContacts(mBinding.userName.getContent());
        }
        if (TextUtils.isEmpty(mBinding.phone.getContent())) {
            MToast.showToast(this, "请输入联系电话");
            return;
        } else {
            bean.setTel(mBinding.phone.getContent());
        }
        if (TextUtils.isEmpty(mBinding.address.getContent())) {
            MToast.showToast(this, "请选择"+mBinding.address.getLabelNameView().getText().toString());
            return;
        }
        if (TextUtils.isEmpty(mBinding.detailedAddress.getContent())) {
            MToast.showToast(this, "请选择详细地址");
            return;
        }
        bean.setRemark(mBinding.note.getEditContent().toString());
        List<CustomBaseBaen> customFieldViewData = mBinding.newaddfieldview.getCustomFieldViewData();
        if (customFieldViewData != null && customFieldViewData.size() > 0) {
            List<OfferApplyListBean> list = new ArrayList<>();
            for (CustomBaseBaen customFieldViewDatum : customFieldViewData) {
                OfferApplyListBean listBean = new OfferApplyListBean();
                listBean.setApplyContent(customFieldViewDatum.getContext() + "");
                listBean.setApplyTitle(customFieldViewDatum.getLabel() + "");
                listBean.setApplyNo(customFieldViewDatum.getNo() + "");
                listBean.setType(type + "");
                list.add(listBean);
            }
            bean.setOfferApplyList(list);
        } else {
            bean.setOfferApplyList(new ArrayList<>());
        }

        //上传图片
        if (mPicAdapter.getDatas().size() > 0) {
            String path = mPicAdapter.getDatas().get(0);
            if (!TextUtils.equals(path, "add")) {
                if (!path.contains("https://")) {//没有上传过的图
                    mModel.uploadAvatarFileModel(this, new File(path), 2);
                } else {//上传过的图
                    uploadAvatarResult(path);
                }
            }else {
                mModel.getSubDataOffer(this, bean);
            }
        } else {
            mModel.getSubDataOffer(this, bean);
        }

    }

    private boolean getDataTime() {
        if (System.currentTimeMillis()<DateUtils.dateTo13Timestamp3(DateUtils.timestampToDate(Long.parseLong(time))+" 23:59:59")){
            return true;
        }
        return  false;
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    /**
     * 图文描述模块view初始化
     */
    private void imtvView() {
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        mBinding.recyclerView.setLayoutManager(manager);
        mPicAdapter = new DemandImgAdapter(this, new ArrayList<>(), 1, manager);
        mBinding.recyclerView.setHasFixedSize(false);
     //   mBinding.recyclerView.addItemDecoration(new GridSpacingItemDecoration(4, ScreenUtils.dip2px(this, 5), true));
        mBinding.recyclerView.setAdapter(mPicAdapter);
        mPicAdapter.setOnItemClick(new AdapterOnItemClick<String>() {
            @Override
            public void onItemClick(String s, int position) {
                if (TextUtils.isEmpty(s)) {
                    PictureSelector.create(DemandOfferActivity.this)
                            .openGallery(PictureMimeType.ofImage())
                            .loadImageEngine(GlideEngine.createGlideEngine())
                            .theme(R.style.picture_default_style)
                            .selectionMode(PictureConfig.MULTIPLE)
                            .compress(true)//是否压缩
                            .minimumCompressSize(3413)//小于3m不压缩
                            .maxSelectNum(mPicAdapter.hasCount())//判断还能选多少张图片
                            .forResult(PictureConfig.CHOOSE_REQUEST);
                } else {
                    List<String> newImg = new ArrayList<>();
                    if (mPicAdapter.getDatas() != null && mPicAdapter.getDatas().size() != 0) {
                        if ("add".equals(mPicAdapter.getDatas().get(mPicAdapter.getDatas().size() - 1))) {
                            for (int i = 0; i < mPicAdapter.getDatas().size(); i++) {
                                if (i != mPicAdapter.getDatas().size() - 1) {
                                    newImg.add(mPicAdapter.getDatas().get(i));
                                }
                            }
                        }
                    }
                    Intent intent = new Intent(getActivity(), PhotoViewActivity.class);
                    intent.putExtra(PhotoViewActivity.KEY_PHOTO_URLS, (Serializable) newImg);
                    intent.putExtra(PhotoViewActivity.KEY_PHOTO_POSITION, position);
                    startActivity(intent);
                }
            }
        });
    }

    /**
     * 数据回显
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                mPicAdapter.setPicData(selectList);
            }
            if (requestCode == 255) {
                if (data == null) {
                    return;
                }
                double lat = data.getDoubleExtra(ZhaoCaiMapAddressActivity.LATITUDE, 0);
                double lon = data.getDoubleExtra(ZhaoCaiMapAddressActivity.LONGTITUDE, 0);
                String address = data.getStringExtra(ZhaoCaiMapAddressActivity.CURRENT_POSITION);
                mBinding.detailedAddress.setContent(address + "");
                bean.setAddrDetail(address);
                bean.setAddrDetailLat(lat + "");
                bean.setAddrDetailLng(lon + "");
            }
        }
    }

    /**
     * 报价详情返回方法
     *
     * @param data
     */
    public void setOfferDetails(OfferDetailsEntitly data) {
        applyCompany = data.getCompNo();
        addrDetailLat = data.getAddrDetailLat();
        addrDetailLng = data.getAddrDetailLng();
        //订单类型   需求类型：1、机械租赁；2、劳务用工；3、材料采购；4、分包计划；5、设备租赁
        bean.setType(data.getType()+"");
        switch (data.getType()) {
            case "1"://机械租赁
                mBinding.llCljxsb.setVisibility(View.VISIBLE);
                mBinding.llLaowu.setVisibility(View.GONE);
                mBinding.llFenbao.setVisibility(View.GONE);
                mBinding.tvTitle.setText("机械名称");
                mBinding.tvNum.setText("租赁量");
                mBinding.demandName.setText(data.getName() + "");//机械名称
                mBinding.demandCom.setText(data.getApplyCompanyName() + "");//企业名称
                mBinding.projectName.setText(data.getProjectName() + "");//项目名称
                mBinding.projectAddress.setText(data.getAddrDetail() + "");//地址
                mBinding.tvDistance.setText(data.getDistance() + "km");//距离
                mBinding.typeName.setText(data.getName() + "");//机械名称
                mBinding.spec.setText(TextUtils.isEmpty(data.getSpec()) ? "/" : data.getSpec());//规格型号
                mBinding.number.setText(data.getSurplus() + data.getUnit() + "");//采购量
                mBinding.planusetime.setText(DateUtils.timestampToDate(Long.parseLong(data.getPlanUseTime())) + "");//使用时间
                time = data.getPlanUseTime();
                mBinding.address.getLabelNameView().setText("所在地");
                mBinding.offerPriceName.setText("送到价（含税）");
               mBinding.llmapaddress.setText("工作地址");
                break;
            case "2"://劳务用工
                mBinding.llCljxsb.setVisibility(View.GONE);
                mBinding.llLaowu.setVisibility(View.VISIBLE);
                mBinding.llFenbao.setVisibility(View.GONE);
                mBinding.demandName.setText(data.getItemProject() + "");//机械名称
                mBinding.demandCom.setText(data.getApplyCompanyName() + "");//企业名称
                mBinding.projectName.setText(data.getProjectName() + "");//项目名称
                mBinding.projectAddress.setText(data.getAddrDetail() + "");//地址
                mBinding.tvDistance.setText(data.getDistance() + "km");//距离
                mBinding.laowuName.setText(data.getPartitionedProjectName() + "");//分布工程
                mBinding.laowuNum.setText(data.getSurplus() + data.getUnit() + "");//工程量
                mBinding.laowuTime.setText(DateUtils.timestampToDate(Long.parseLong(data.getPlanUseTime())) + "");//开工时间
                mBinding.laowuText.setText(data.getMemo() + "");//项目特征描述
                time = data.getPlanUseTime();
                mBinding.address.getLabelNameView().setText("工队地址");
                mBinding.offerPriceName.setText("分包价（含税）");
                bean.setItemProject(data.getItemProject()+"");
                new ExpandedTextView().showTextPick(mBinding.laowuText, mBinding.tvAn3);
                mBinding.llmapaddress.setText("项目地址");
                break;
            case "3"://材料采购
                mBinding.llCljxsb.setVisibility(View.VISIBLE);
                mBinding.llLaowu.setVisibility(View.GONE);
                mBinding.llFenbao.setVisibility(View.GONE);
                mBinding.demandName.setText(data.getName() + "");//材料名称
                mBinding.demandCom.setText(data.getApplyCompanyName() + "");//企业名称
                mBinding.projectName.setText(data.getProjectName() + "");//项目名称
                mBinding.projectAddress.setText(data.getAddrDetail() + "");//地址
                mBinding.tvDistance.setText(data.getDistance() + "km");//距离
                mBinding.tvTitle.setText("材料名称");
                mBinding.tvNum.setText("采购量");
                mBinding.typeName.setText(data.getName() + "");//材料名称
                mBinding.spec.setText(TextUtils.isEmpty(data.getSpec()) ? "/" : data.getSpec());//规格型号
                mBinding.number.setText(data.getSurplus() + data.getUnit() + "");//采购量
                mBinding.planusetime.setText(DateUtils.timestampToDate(Long.parseLong(data.getPlanUseTime())) + "");//使用时间
                time =data.getPlanUseTime();
                mBinding.address.getLabelNameView().setText("仓储地址");
                mBinding.offerPriceName.setText("送到价（含税）");
                mBinding.llmapaddress.setText("收货地址");
                break;
            case "4"://分包计划
                mBinding.llCljxsb.setVisibility(View.GONE);
                mBinding.llLaowu.setVisibility(View.GONE);
                mBinding.llFenbao.setVisibility(View.VISIBLE);
                //     分包计划：所属工程、施工内容、项目特征描述、工程量、要求开工/完工时间
                mBinding.demandName.setText(data.getItemProject()+"");//分包计划名称
                mBinding.demandCom.setText(data.getApplyCompanyName()+"");//企业名称
                mBinding.projectName.setText(data.getProjectName()+"");//项目名称
                mBinding.projectAddress.setText(data.getAddrDetail()+"");//地址
                mBinding.tvDistance.setText(data.getDistance()+"km");//距离
                mBinding.address.getLabelNameView().setText("工队地址");
                mBinding.offerPriceName.setText("分包价（含税）");
                mBinding.engineering.setText(data.getBelongProjectStr()+"");
                mBinding.engineeringNum.setText(data.getSurplus()+data.getItemUnit()+"");
                mBinding.startTime.setText(DateUtils.timestampToDate(Long.parseLong(data.getStartDate()))+"至"+data.getEndDate());
                mBinding.constructionContent.setText(data.getEngineerContent()+"");
                mBinding.projectCharacteristics.setText(data.getItemMemo()+"");
                time = data.getStartDate();
                bean.setItemProject(data.getItemProject()+"");
                new ExpandedTextView().showTextPick(mBinding.constructionContent, mBinding.tvAn1);
                new ExpandedTextView().showTextPick(mBinding.projectCharacteristics, mBinding.tvAn2);
                mBinding.llmapaddress.setText("项目地址");
                break;
            case "5"://设备租赁
                mBinding.llCljxsb.setVisibility(View.VISIBLE);
                mBinding.llLaowu.setVisibility(View.GONE);
                mBinding.llFenbao.setVisibility(View.GONE);
                mBinding.tvTitle.setText("设备名称");
                mBinding.tvNum.setText("租赁量");
                mBinding.demandName.setText(data.getName() + "");//设备名称
                mBinding.demandCom.setText(data.getApplyCompanyName() + "");//企业名称
                mBinding.projectName.setText(data.getProjectName() + "");//项目名称
                mBinding.projectAddress.setText(data.getAddrDetail() + "");//地址
                mBinding.tvDistance.setText(data.getDistance() + "km");//距离
                mBinding.typeName.setText(data.getName() + "");//设备名称
                mBinding.spec.setText(TextUtils.isEmpty(data.getSpec()) ? "/" : data.getSpec());//规格型号
                mBinding.number.setText(data.getSurplus() + data.getUnit() + "");//采购量
                mBinding.planusetime.setText(DateUtils.timestampToDate(Long.parseLong(data.getPlanUseTime())) + "");//使用时间
                time = data.getPlanUseTime();
                mBinding.address.getLabelNameView().setText("所在地");
                mBinding.offerPriceName.setText("送到价（含税）");
                mBinding.llmapaddress.setText("工作地址");
                break;
        }
        //报价信息是否存在
        if (data.getNoticeOffer() != null) {
            NoticeOfferBean noticeOffer = data.getNoticeOffer();
            //报价人名称
            mBinding.offerName.setContent(noticeOffer.getBidderName() + "");
            bean.setBidderName(noticeOffer.getBidderName() + "");
            //报价
            mBinding.price.setText(noticeOffer.getPrice() + "");
            //联系人
            mBinding.userName.setContent(noticeOffer.getCreatorName() + "");
            //联系电话
            mBinding.phone.setContent(noticeOffer.getTel() + "");
            //地址
            if (!TextUtils.isEmpty(noticeOffer.getAreaName())){
                mBinding.address.setContent(noticeOffer.getProvinceName()+"/"+ noticeOffer.getCityName()+"/"+noticeOffer.getAreaName() + "");
            }else {
                mBinding.address.setContent(noticeOffer.getProvinceName()+"/"+ noticeOffer.getCityName()+ "");
            }
            bean.setProvinceName(noticeOffer.getProvinceName() + "");
            bean.setProvinceNo(noticeOffer.getProvinceNo() + "");
            bean.setCityName(noticeOffer.getCityName() + "");
            bean.setCityNo(noticeOffer.getCityNo() + "");
            bean.setAreaName(noticeOffer.getAreaName() + "");
            bean.setAreaNo(noticeOffer.getAreaNo() + "");
            addresscode = noticeOffer.getProjectAddress();
            bean.setProjectAddressName( mBinding.address.getContent()+"");
            //详细地址
            mBinding.detailedAddress.setContent(noticeOffer.getAddrDetail() + "");
            bean.setAddrDetail(noticeOffer.getAddrDetail() + "");
            bean.setAddrDetailLat(noticeOffer.getAddrDetailLat() + "");
            bean.setAddrDetailLng(noticeOffer.getAddrDetailLng() + "");
            //备注
            mBinding.note.setEditContent(noticeOffer.getRemark() + "");
            //自定义字段
            if (noticeOffer.getOfferApplyList() != null && noticeOffer.getOfferApplyList().size() > 0) {
                List<OfferApplyListBean> offerApplyList = noticeOffer.getOfferApplyList();
                List<CustomBaseBaen> CustomBaselist = new ArrayList<>();
                for (OfferApplyListBean offerApplyListBean : offerApplyList) {
                    CustomBaseBaen bean = new CustomBaseBaen();
                    bean.setLabel(offerApplyListBean.getApplyTitle());
                    bean.setContext(offerApplyListBean.getApplyContent());
                    bean.setNo(offerApplyListBean.getApplyNo());
                    CustomBaselist.add(bean);
                }
                mBinding.newaddfieldview.setData(CustomBaselist);
            }
            //图片展示
            if (!TextUtils.isEmpty(noticeOffer.getFile())) {
                boolean isImage = getIsImage(noticeOffer.getFile());
                List<LocalMedia> selectList = new ArrayList<>();
                if (isImage) {
                    LocalMedia localmedia = new LocalMedia();
                    localmedia.setRealPath(noticeOffer.getFile() + "");
                    selectList.add(localmedia);
                }
                mPicAdapter.setPicData(selectList);
                bean.setFile(noticeOffer.getFile() + "");
                bean.setFileName(noticeOffer.getFileName() + "");
            }
            bean.setGoodsId(noticeOffer.getGoodsId() + "");
        } else {
            //报价人名称
            mBinding.offerName.setContent(name + "");
            bean.setBidderName(name + "");
            bean.setGoodsId(id + "");
        }
        bean.setApproachTime(time);
        bean.setProjectId(data.getProjectId());
        bean.setProjectName(data.getProjectName());
        bean.setBusinessPlanId(data.getBusinessPlanId());
        bean.setName(mBinding.demandName.getText().toString());
    }

    //判断是不是图片
    private boolean getIsImage(String fileUrl) {
        if (fileUrl.contains(".jpg") || fileUrl.contains(".png")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 返回处理方法
     */
    private void backView() {
        finish();
    }

    /**
     * 返回图片地址路径
     */
    public void uploadAvatarResult(String filePath) {
        if (!TextUtils.isEmpty(filePath)) {
            bean.setFile(filePath);
            int indexOf = filePath.lastIndexOf("/");//当前字符最后一次出现的位置
            bean.setFileName(filePath.substring(indexOf+1, filePath.length()));//截取字符串设置
        }
        mModel.getSubDataOffer(this, bean);
    }

    /**
     * 返回的地址信息
     *
     * @param data
     */
    public void setAddressList(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
        this.addressEntityList = data;
        initJsonData();
        showCityPop();
    }

    //组装省市区数据信息
    private void initJsonData() {
        for (int i = 0; i < addressEntityList.size(); i++) {
            ArrayList areaList = new ArrayList<>();
            opt2.add(addressEntityList.get(i).getChildren());
            for (int j = 0; j < addressEntityList.get(i).getChildren().size(); j++) {
                List<AddressEntity> cityAreaList = new ArrayList<>();
                List<AddressEntity> children = addressEntityList.get(i).getChildren().get(j).getChildren();
                if (children == null || children.size() == 0) {
                    cityAreaList.add(null);
                } else {
                    cityAreaList.addAll(children);
                }
                areaList.add(cityAreaList);
            }
            opt3.add(areaList);
        }
    }

    /**
     * 地址选择器
     */
    public void showCityPop() {
        //条件选择器
        CityPickView cityOption = new CityPickBuilder(this, (options1, options2, options3, v) -> {
            String areaName;
            if (opt3.get(options1).get(options2).get(options3) == null) {
                areaName = "";
                bean.setAreaNo("");
                bean.setAreaName(areaName + "");
            } else {
                areaName = opt3.get(options1).get(options2).get(options3).getName();
                cityPosition3 = options3;
                bean.setAreaNo(opt3.get(options1).get(options2).get(options3).getNo());
                bean.setAreaName(areaName + "");
            }
            String provinceName = addressEntityList.get(options1).getName();
            String cityName = opt2.get(options1).get(options2).getName();
            if (!TextUtils.isEmpty(cityName)) {
                if (!TextUtils.isEmpty(areaName)) {
                    mBinding.address.setContent(provinceName + "/" + cityName + "/" + areaName);
                } else {
                    mBinding.address.setContent(provinceName + "/" + cityName);
                }
            } else {
                mBinding.address.setContent(provinceName);
            }
            bean.setProvinceName(provinceName);
            bean.setProvinceNo(addressEntityList.get(options1).getId());
            bean.setCityName(cityName + "");
            bean.setCityNo(opt2.get(options1).get(options2).getNo());
            bean.setAddrDetail("");
            bean.setAddrDetailLng("");
            bean.setAddrDetailLat("");
            mBinding.detailedAddress.setContent("");
            addresscode.add(addressEntityList.get(options1).getId());
            if (!TextUtils.isEmpty(opt2.get(options1).get(options2).getNo())){
                addresscode.add(opt2.get(options1).get(options2).getNo());
            }
            if (opt3.get(options1).get(options2).get(options3)!=null&&!TextUtils.isEmpty(opt3.get(options1).get(options2).get(options3).getNo())){
                addresscode.add(opt3.get(options1).get(options2).get(options3).getNo());
            }
            bean.setProjectAddressName( mBinding.address.getContent()+"");
            mModel.getLocationLL(this, bean.getProvinceNo(), bean.getCityNo(), bean.getAreaNo());
        }).setCancelText("取消").setSubCalSize(15)
                .setCancelColor(Color.parseColor("#666666"))
                .setSubmitText("确定")
                .setSubmitColor(Color.parseColor("#5792FD")).build();
        cityOption.setPicker(addressEntityList, opt2, opt3);
        cityOption.setSelectOptions(cityPosition1, cityPosition2, cityPosition3);
        cityOption.show();
    }

    /**
     * 返回经纬度信息
     *
     * @param data
     */
    public void updateAddress(MapLongAndLatEntity data) {
        if (!TextUtils.isEmpty(data.getLat())) {
            if (!TextUtils.isEmpty(data.getLng())) {
                if (!TextUtils.isEmpty(data.getLat())) {
                    lat = data.getLat();
                } else {
                    lat = "0";
                }
                if (!TextUtils.isEmpty(data.getLng())) {
                    lng = data.getLng();
                } else {
                    lng = "0";
                }
            }
        }
    }

    public void setSubDataOffer(String name) {
        if (acttype.equals("2")){
            new CommonDialog(getActivity()).builder().setPositiveButton("继续浏览").setNegativeButton("进入我的报价").setLiftTextColoe(this.getResources().getColor(R.color.color_secondary_blue))
                    .setTitle("温馨提示")
                    .setMsg("您的报价信息已经提交成功,\n请留意后期通知")
                    .setDialogCallBack(new CommonDialog.DialogCallBack() {
                        @Override
                        public void onRightClick() {
                            finish();
                        }

                        @Override
                        public void onLeftClick() {
                            finish();
                            OfferQueryListActivity.start(getActivity());
                        }
                    }).show();
        }else {
            MToast.showToast(this,"您的报价信息已经提交成功,请留意后期通知");
            finish();
        }
    }

    @Override
    public void onBackPressed() {
        backView();
    }

    //未登录弹框跳转认证
    public void showDialog3() {
        View view = View.inflate(this, R.layout.dialog_two_btn, null);
        RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("温馨提示");
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        tv_logout_confirm.setText("确定");
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        tv_message.setText("未登录，请先登录。");
        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityJump.startActivity(PathLogin.VerificationCodeLoginActivity);
                roundCornerDialog.dismiss();
            }
        });
        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }
}
